var deviceone = require("deviceone");
var app = deviceone.sm("do_App");
var jpush = deviceone.sm("do_JPush");
var nf = deviceone.sm("do_Notification");
var global = deviceone.sm("do_Global");
var pushUrl = require("url/push");
var userTopic = require("helper/userTopic");
var deviceHelper = require("helper/device");
var openHelper = require("helper/open");

var userUtil = require("helper/user");
var imApiHelper = require("helper/imApi");
var imHelper = require("helper/im");
var urlUser = require("url/user");
var pubUrl = require("url/pub");
var urlMall = require("url/mall");

var upgradeHelper = require("helper/upgrade");
var httpSilence = require("helper/httpSilence");
var qqCacher = require("helper/qqCacher");

function getRegistrationID(){
	return jpush.getRegistrationID();
}
module.exports.getRegistrationID = getRegistrationID;
/** 新增用户设备的推送注册id **/
module.exports.handleAudience = function(){
	var registrationID = getRegistrationID();
	if(registrationID && registrationID!=null && registrationID!=="undefined"){
		var http = require("helper/http");
		http.post(pushUrl.audience.add,{"registrationID":registrationID});
	}
};
function updateByVersion(){
	httpSilence.post(pubUrl.upgrade.version.getByVersion,{"currVersion":qqCacher.currentContentVersion(),"platform":deviceHelper.getOsType()},function(dataVersion){
		var updFile = dataVersion.updFile;
		var toVersion = dataVersion.toVersion;
		upgradeHelper.doUpdateByVersion(updFile,toVersion,function(){
		});
	});
}
app.on("firePushCustomMessage",function(data){
	var extra = data.extra;
	if(extra && extra!=null && extra!==""){
		var extraJson = JSON.parse(extra);
		if(extraJson && extraJson.pushBizz && extraJson.pushBizz!==""){
			if(extraJson.pushBizz==="101"){//app升级
				var osType = deviceHelper.getOsType();
				var pubUpgrade = JSON.parse(extraJson.pushPubUpgradeAndroid);
				if(osType==="1"){
					pubUpgrade = JSON.parse(extraJson.pushPubUpgradeAndroid);
				}else if(osType==="2"){
					pubUpgrade = JSON.parse(extraJson.pushPubUpgradeIos);
				}else{
					return;
				}
				app.fire("fireAppUpgrade",pubUpgrade);
			}else if(extraJson.pushBizz==="104"){//根据客户端内容版本进行增量升级
				updateByVersion();
			}else if(extraJson.pushBizz==="105"){//根据客户端原生版本安装升级
				updateByVersion();
				var nativeVersion = extraJson.pushPubUpgradeNativeVersion;
				var updUrl = extraJson.pushPubUpgradeWxzAddressAndroid;
				if(deviceHelper.getOsType()==="2"){
					updUrl = extraJson.pushPubUpgradeWxzAddressIos;
				}
				if(nativeVersion&&nativeVersion!=null&&updUrl&&updUrl!=null){
					var isNew = upgradeHelper.isNewVersion(qqCacher.currentNativeVersion(), nativeVersion);
					if(isNew&&isNew==true){
						var contentList = {};
						if(extraJson.pushPubUpgradeContentList&&extraJson.pushPubUpgradeContentList!==""){
							contentList = JSON.parse(extraJson.pushPubUpgradeContentList);
						}
						var mTimer = deviceone.mm("do_Timer", "timer4upgradeNative2", "app");
						mTimer.delay = 0;
						mTimer.interval = 1000;
						var maxVal = 8;
						if (!mTimer.isStart()) {
						    mTimer.start();
						}
						mTimer.on("tick", function(data,e) {
						    if (maxVal <= 0) {
						    	mTimer.stop();
						    	openHelper.open("source://view/public/upgrade/updNative.ui",{"contentList":contentList,"updUrl":updUrl});
						    }
							maxVal--;
						});
					}
				}
			}else if(extraJson.pushBizz==="201"){//友事
				//DO Nothing
			}else if(extraJson.pushBizz==="301"){//有人评论了我的内容
				if(userUtil.loginToken()!==""&&userUtil.userId()===extraJson.bePushUserId){
					app.fire("qq_fire_show_msg_tip");//消息红点
				}
				if(extraJson.pushCommentId!=null){
					imHelper.storeUnReadCommentId({myId:extraJson.bePushUserId, commentId:extraJson.pushCommentId});
				}
				imHelper.increaseUnReadCount2Her({herId:"comment", myId:extraJson.bePushUserId});//V2评论我的数量+1
				imHelper.increaseMyUnReadCount({myId:extraJson.bePushUserId});//V2我的未读消息数量+1
			}else if(extraJson.pushBizz==="302"){//有人回复了我的评论
				if(userUtil.loginToken()!==""&&userUtil.userId()===extraJson.bePushUserId){
					app.fire("qq_fire_show_msg_tip");//消息红点
				}
				if(extraJson.pushCommentId!=null){
					imHelper.storeUnReadCommentId({myId:extraJson.bePushUserId, commentId:extraJson.pushCommentId});
				}
				imHelper.increaseUnReadCount2Her({herId:"comment", myId:extraJson.bePushUserId});//V2评论我的数量+1
				imHelper.increaseMyUnReadCount({myId:extraJson.bePushUserId});//V2我的未读消息数量+1
			}else if(extraJson.pushBizz==="401"){//点赞
				if(userUtil.loginToken()!==""&&userUtil.userId()===extraJson.bePushUserId){
					app.fire("qq_fire_show_msg_tip");//消息红点
				}
				imHelper.increaseUnReadCount2Her({herId:"zan", myId:extraJson.bePushUserId});//V2点赞我的数量+1
				imHelper.increaseMyUnReadCount({myId:extraJson.bePushUserId});//V2我的未读消息数量+1
			}else if(extraJson.pushBizz==="501"){//被申请加好友时的推送
				if(userUtil.loginToken()!==""&&userUtil.userId()===extraJson.bePushUserId){
					app.fire("qq_fire_show_msg_tip");//消息红点
				}
				imHelper.increaseMyUnReadCount({myId:extraJson.bePushUserId});//V2我的未读消息数量+1
			}else if(extraJson.pushBizz==="502"){//用户处于后台时的IM消息推送通知
				app.fire("qq_fire_show_msg_tip");//消息红点
			}else if(extraJson.pushBizz==="503"){//推送被迫下线
				if(userUtil.loginToken()!==""&&userUtil.userId()===extraJson.pushUserOffUserId){
					userUtil.loginToken("");
					imApiHelper.logoutIM(function(){
						nf.alert("账号在其它手机登录，您将被迫下线", function() {
							app.fire("fireUserLogout");
						});
					});
				}
			}else if(extraJson.pushBizz==="504"){//身份认证通知
				if(extraJson.pushBizzStatus && extraJson.pushBizzStatus!=null && extraJson.pushBizzStatus!=="undefined"){
					userUtil.hasIdentityAuth(extraJson.pushBizzStatus);
				}
			}else if(extraJson.pushBizz==="505"){//社区住户认证通知
				if(extraJson.pushBizzStatus && extraJson.pushBizzStatus!=null && extraJson.pushBizzStatus!=="undefined"){
					userUtil.hasCommunityAuth(extraJson.pushBizzStatus);
				}
			}else if(extraJson.pushBizz==="506"){//被邀请加入圈子
				if(userUtil.loginToken()!==""&&userUtil.userId()===extraJson.bePushUserId){
					app.fire("qq_fire_show_msg_tip");//消息红点
				}
				imHelper.increaseMyUnReadCount({myId:extraJson.bePushUserId});//V2我的未读消息数量+1
			}else if(extraJson.pushBizz==="507"){//有人申请加入你的圈子
				if(userUtil.loginToken()!==""&&userUtil.userId()===extraJson.bePushUserId){
					app.fire("qq_fire_show_msg_tip");//消息红点
				}
				imHelper.increaseMyUnReadCount({myId:extraJson.bePushUserId});//V2我的未读消息数量+1
			}else if(extraJson.pushBizz==="601"){//买家下单
				/*
				if(extraJson.pushOrderBizzType&&extraJson.pushOrderBizzType==="1"){
					openHelper.openHKSellerOrder(extraJson.pushOrderNo);
				}else{
					openHelper.openSellerOrder(extraJson.pushOrderNo);
				}
				*/
			}else if(extraJson.pushBizz==="602"){//买家付款
				if(userUtil.loginToken()!==""){
					if(extraJson.pushOrderBizzType&&extraJson.pushOrderBizzType==="1"){
						nf.toast("您有新订单");
						//openHelper.openHKSellerOrder(extraJson.pushOrderNo);
					}else if(extraJson.pushOrderBizzType&&extraJson.pushOrderBizzType==="6"){//圈商
						if(extraJson.pushOrderSupplys!==""&&extraJson.pushOrderSupplys.indexOf(";"+userUtil.userId()+";")>=0){
							nf.toast("您有新订单");
							imHelper.playOrderTip4Seller();
							//openHelper.open("source://view/qshop/manage/shop/myShop.ui");
						}
					}else {
						if(extraJson.pushOrderSupplys!==""&&extraJson.pushOrderSupplys.indexOf(";"+userUtil.userId()+";")>=0){
							nf.toast("您有新订单");
						}
					}
				}
			}else if(extraJson.pushBizz==="603"){//卖家发货
				//openHelper.openBuyerOrder(extraJson.pushOrderNo);
			}else if(extraJson.pushBizz==="604"){//买家签收
				/*
				if(extraJson.pushOrderBizzType&&extraJson.pushOrderBizzType==="1"){
					openHelper.openHKSellerOrder(extraJson.pushOrderNo);
				}else{
					openHelper.openSellerOrder(extraJson.pushOrderNo);
				}
				*/
			}else if(extraJson.pushBizz==="605"){//买家已取消
				/*
				if(extraJson.pushOrderBizzType&&extraJson.pushOrderBizzType==="1"){
					openHelper.openHKSellerOrder(extraJson.pushOrderNo);
				}else{
					openHelper.openSellerOrder(extraJson.pushOrderNo);
				}
				*/
			}else if(extraJson.pushBizz==="606"){//卖家已取消
				//openHelper.openBuyerOrder(extraJson.pushOrderNo);
			}else if(extraJson.pushBizz==="607"){//买家申请退货
				/*
				if(extraJson.pushOrderBizzType&&extraJson.pushOrderBizzType==="1"){
					openHelper.openHKSellerOrder(extraJson.pushOrderNo);
				}else{
					openHelper.openSellerOrder(extraJson.pushOrderNo);
				}
				*/
			}else if(extraJson.pushBizz==="608"){//卖家同意退货
				//openHelper.openBuyerOrder(extraJson.pushOrderNo);
			}else if(extraJson.pushBizz==="609"){//买家申请退款
				nf.toast("买家申请退款，请及时处理");
			}else if(extraJson.pushBizz==="610"){//同意退款
				nf.toast("您申请的退款已同意，请留意余额");
			}else if(extraJson.pushBizz==="611"){//卖家拒绝退款
				nf.toast("卖家拒绝退款，请保持沟通或联系圈圈客服");
			}else if(extraJson.pushBizz==="701"){//有新的配送单
				if(userUtil.loginToken()!==""&&extraJson.pushSendOrderUserId==userUtil.userId()){
					nf.toast("您有新配送单，请及时配送");
					//openHelper.open("source://view/sendDelivery/orderDetail.ui", {orderNo : extraJson.pushOrderNo});
				}
			}else if(extraJson.pushBizz==="901"){//系统后台推送
				app.fire("qq_fire_show_msg_tip");//消息红点
				imHelper.addAdminPushCahce(extraJson.pushBizzTitle, extraJson.pushBizzMsg, extraJson.pushBizzUrl);
				imHelper.increaseMyUnReadCount();//V2我的未读消息数量+1
			}
			else if(extraJson.pushBizz==="902"){//打开店铺
				var shopId = extraJson.shopId;
				if(shopId > 0){
					openHelper.open("source://view/qshop/detail/main.ui",{shopId:shopId});
				}
			}
			else if(extraJson.pushBizz==="903"){//打开活动
				var activityId = extraJson.activityId;
				if(activityId > 0){
					openHelper.open("source://view/activity/detail/msgBorderView.ui",{id:activityId});
				}
			}
			else if(extraJson.pushBizz==="802"){//打开我的优惠券
				if(userUtil.loginToken()!==""&&userUtil.userId()&&userUtil.userId()!==""){
					if(extraJson.bePushUserId!==""&&extraJson.bePushUserId.indexOf(";"+userUtil.userId()+";")>=0){
						openHelper.open("source://view/coupon/manage/list.ui");
					}
				}
			}
		}
	}
});